/**
 * 
 */
package mx.netbrain.bean;

import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import mx.netbrain.dao.UserDao;
import mx.netbrain.dao.impl.UserDaoImpl;
import mx.netbrain.model.Status;
import mx.netbrain.model.User;

/**
 * @author julian
 * 
 */
public class UserBean {

	private String name;
	private String lastname;
	private String maidenname;
	private String username;
	private String password;
	private String mail;
	private List<User> listUser;
	private List<User> filteredUser;
	private Status status = new Status();
	private User user;

	public UserBean() {
	}

	public void prepareUser(Long id) {
		UserDao userDao = new UserDaoImpl();
		user = userDao.findById(id);
	}

	public void save() {
		UserDao userDao = new UserDaoImpl();
		user = new User();
		user.setName(name);
		user.setLastname(lastname);
		user.setMaidenname(maidenname);
		user.setUsername(username);
		user.setPassword(password);
		user.setMail(mail);
		user.setRegister(new Date());
		status.setId(1);
		user.setStatus(status);
		user.setType(2);
		userDao.save(user);
		user = new User();
		status = new Status();
		name = new String();
		lastname = new String();
		maidenname = new String();
		username = new String();
		password = new String();
		mail = new String();
		FacesContext context = FacesContext.getCurrentInstance();
		context.addMessage(null, new FacesMessage(
				"Datos actualizados correctamente!", "Actualizado"));
	}

	public void edit() {
		UserDao userDao = new UserDaoImpl();
		userDao.update(user);
		FacesContext context = FacesContext.getCurrentInstance();
		context.addMessage(null, new FacesMessage(
				"Datos actualizados correctamente!", "Actualizado"));
		user = new User();
	}

	public void delete() {
		UserDao userDao = new UserDaoImpl();
		status.setId(2);
		user.setStatus(status);
		userDao.delete(user);
		FacesContext context = FacesContext.getCurrentInstance();
		context.addMessage(null, new FacesMessage(
				"Datos actualizados correctamente!", "Actualizado"));
		status = new Status();
		user = new User();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getMaidenname() {
		return maidenname;
	}

	public void setMaidenname(String maidenname) {
		this.maidenname = maidenname;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public List<User> getListUser() {
		UserDao userDao = new UserDaoImpl();
		listUser = userDao.listAll();
		return listUser;
	}

	public void setListUser(List<User> listUser) {
		this.listUser = listUser;
	}

	public List<User> getFilteredUser() {
		return filteredUser;
	}

	public void setFilteredUser(List<User> filteredUser) {
		this.filteredUser = filteredUser;
	}

	public User getUser() {
		if (user == null) {
			user = new User();
		}
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}
